﻿Imports System.IO
Imports System.Xml.Serialization

Namespace Utilities
    Public Class FileTools

        Public Shared Sub ProcessFilesRecursively(ByVal directory As String, ByVal filter As String, ByVal processfile As Action(Of String))
            Dim searchOptions As IO.SearchOption = IO.SearchOption.AllDirectories
            ProcessFilesRecursively(directory, filter, processfile, searchOptions)
        End Sub

        Public Shared Sub ProcessFilesRecursively(ByVal directory As String, ByVal filter As String, ByVal processfile As Action(Of String), ByVal searchOptions As IO.SearchOption)
            If My.Computer.FileSystem.FileExists(directory) Then
                processfile(directory)
            ElseIf My.Computer.FileSystem.DirectoryExists(directory) Then
                ProcessDirectory(directory, filter, processfile, searchOptions)
            End If
        End Sub

        Private Shared Sub ProcessDirectory(ByVal directory As String, ByVal extension As String, ByVal processfile As Action(Of String), ByVal searchOptions As IO.SearchOption)
            Try
                IO.Directory.GetFiles(directory, extension, searchOptions).ToList.ForEach(Sub(fileName) processfile(fileName))
            Catch ex As Exception

            End Try
        End Sub
    End Class
End Namespace